package org.afs.gescol.core.service.implementations;

import org.afs.gescol.core.dao.interfaces.SchoolClassDao;
import org.afs.gescol.core.dto.classes.SchoolClassDto;
import org.afs.gescol.core.model.data.SchoolClass;
import org.afs.gescol.core.service.interfaces.ClassManagementServices;
import org.afs.gescol.core.service.util.MessageKeyUtil;
import org.scub.foundation.framework.base.exception.TechnicalException;
import org.scub.foundation.framework.base.mapping.util.MapperDozerBean;
import org.scub.foundation.framework.base.messagesource.MessageSourceUtil;

public class ClassManagementServicesImpl implements ClassManagementServices {

    private MessageSourceUtil messageSourceUtil;

    private MapperDozerBean mapperDozerBean;

    private SchoolClassDao schoolClassDao;

    @Override
    public SchoolClassDto createOrUpdateSchoolClass(SchoolClassDto schoolClass) {
        chekSchoolClass(schoolClass);
        SchoolClass managedSchoolClass = null;
        if (schoolClass.getSchoolClassId() != null) {
            managedSchoolClass = updateSchoolClass(schoolClass);
        } else {
            managedSchoolClass = createSchoolClass(schoolClass);
        }
        return (SchoolClassDto) mapperDozerBean.map(managedSchoolClass, SchoolClassDto.class);
    }

    @Override
    public void removeSchoolClass(Long schoolClassId) {
        final SchoolClass dataFromDb = getData(schoolClassId);
        schoolClassDao.deleteSchoolClass(dataFromDb);
    }

    private SchoolClass createSchoolClass(SchoolClassDto schoolClass) {
        final SchoolClass dataToDb = new SchoolClass();
        mapperDozerBean.map(schoolClass, dataToDb);
        schoolClassDao.createSchoolClass(dataToDb);
        return dataToDb;
    }

    private SchoolClass updateSchoolClass(SchoolClassDto schoolClass) {
        final SchoolClass dataFromDb = getData(schoolClass.getSchoolClassId());
        mapperDozerBean.map(schoolClass, dataFromDb);
        return dataFromDb;
    }

    private SchoolClass getData(Long schoolClassId) {
        final SchoolClass dataFromDb = schoolClassDao.getSchoolClassById(schoolClassId);
        if (dataFromDb == null) {
            throw new TechnicalException(messageSourceUtil.get(MessageKeyUtil.ERROR_NOT_FOUND_SCHOOL_CLASS));
        }
        return dataFromDb;
    }

    private void chekSchoolClass(SchoolClassDto schoolClass) {
        if (schoolClass == null) {
            throw new TechnicalException(messageSourceUtil.get(MessageKeyUtil.ERROR_NULL_SCHOOL_CLASS));
        }
        // TODO Auto-generated method stub
        
    }

    /**
     * Set the value of messageSourceUtil.
     * @param messageSourceUtil the messageSourceUtil to set
     */
    public void setMessageSourceUtil(MessageSourceUtil messageSourceUtil) {
        this.messageSourceUtil = messageSourceUtil;
    }

    /**
     * Set the value of mapperDozerBean.
     * @param mapperDozerBean the mapperDozerBean to set
     */
    public void setMapperDozerBean(MapperDozerBean mapperDozerBean) {
        this.mapperDozerBean = mapperDozerBean;
    }

    /**
     * Set the value of schoolClassDao.
     * @param schoolClassDao the schoolClassDao to set
     */
    public void setSchoolClassDao(SchoolClassDao schoolClassDao) {
        this.schoolClassDao = schoolClassDao;
    }

}
